<template>
    <div class="quotation-sheet-page">
        <el-form ref="quotationSheetForm" :model="quotationSheetForm" :inline="true"  class="demo-form-inline" style="border-bottom:1px solid #ccc;">
            <!-- 法人名称 -->
            <el-form-item :label="nowLangue.legalName" v-if="!powerFlag" class="selectMB10">
                <el-input size="mini" v-if="legalPersonDropDown.length == 1" disabled v-model="quotationSheetForm.legalPersonName"></el-input>
                <el-select size="mini" clearable v-else  v-model="quotationSheetForm.legalPersonName" :placeholder="nowLangue.pleaseSelect" @change="changeLegalPerson">
                    <el-option v-for="(legalPerson,index) in legalPersonDropDown" :key="index" :label="legalPerson.legalPersonName" :value="legalPerson.legalPersonName">
                        <span>{{ legalPerson.legalPersonName }}</span>
                    </el-option>
                </el-select>
            </el-form-item>
            <!--供应商名称-->
            <el-form-item :label="nowLangue.supplierName" class="selectMB10">
                <el-select size="mini" clearable v-model="quotationSheetForm.vendor" :placeholder="nowLangue.pleaseSelect"  v-if="!powerFlag">
                    <el-option v-for="(vendor,index) in quotationSheetForm.selectDropdown" :key="index" :label="vendor.partnerName" :value="vendor.partnerCode"></el-option>
                </el-select>
                <el-input size="mini" v-model="quotationSheetForm.vendor" placeholder="" v-else disabled></el-input>
            </el-form-item>
            <!--报价单号-->
            <el-form-item :label="nowLangue.quotationNo" class="selectMB10">
                <el-input size="mini" v-model="quotationSheetForm.quotationNumber" :placeholder="nowLangue.pleaseImport+nowLangue.quotationNo"></el-input>
            </el-form-item>
            <!--备件编码-->
            <el-form-item :label="nowLangue.spareCode" class="selectMB10">
                <el-input size="mini" v-model="quotationSheetForm.spareCoding" :placeholder="nowLangue.pleaseImport+nowLangue.spareCode"></el-input>
            </el-form-item>
            <!--备件名称-->
            <el-form-item :label="nowLangue.spareName" class="selectMB10">
                <el-input size="mini" v-model="quotationSheetForm.spareName" :placeholder="nowLangue.pleaseImport+nowLangue.spareCode"></el-input>
            </el-form-item>
            <!--报价状态-->
            <el-form-item :label="nowLangue.biddingStatus" class="selectMB10">
                <el-select size="mini" clearable v-model="quotationSheetForm.biddingStatus" :placeholder="nowLangue.pleaseSelect">
                    <el-option v-for="(biddingStatus,index) in selectDropdown.biddingStatus" :key="index" :label="biddingStatus.label" :value="biddingStatus.value"></el-option>
                </el-select>
            </el-form-item>
            <!--<el-form-item label="报价类型" class="selectMB10">
                <el-select size="mini" v-model="quotationSheetForm.biddingType" :placeholder="nowLangue.pleaseSelect">
                    <el-option v-for="biddingType in selectDropdown.type" :label="biddingType.label" :value="biddingType.value"></el-option>
                </el-select>
            </el-form-item>-->
            <!--报价日期-->
            <el-form-item :label="nowLangue.quotationDate" class="selectMB10">
                <el-date-picker type="date" size="mini" :editable="false" v-model="quotationSheetForm.createStartTime"  @change="selectedCreateStartTime" :placeholder="nowLangue.pleaseSelect"></el-date-picker>
                &nbsp;
                <!--至-->
                {{nowLangue.to}}
                &nbsp;
                <el-date-picker type="date" size="mini" :editable="false" v-model="quotationSheetForm.createEndTime"  @change="selectedCreateEndTime" :placeholder="nowLangue.pleaseSelect"></el-date-picker>
            </el-form-item>
            <!--报价生效日期-->
            <el-form-item :label="nowLangue.takeEffectiveDateOfQuotation" class="selectMB10">
                <el-date-picker type="date" size="mini" :editable="false" v-model="quotationSheetForm.startTime"  :placeholder="nowLangue.pleaseSelect" @change="selectedStartTime" class="selectMB10"></el-date-picker>
            </el-form-item>
            <!--报价失效日期-->
            <el-form-item :label="nowLangue.expiryDateOfQuotation" class="selectMB10">
                <el-date-picker type="date" size="mini" :editable="false" v-model="quotationSheetForm.endTime"  :placeholder="nowLangue.pleaseSelect" @change="selectedEndTime" class="selectMB10"></el-date-picker>
            </el-form-item>
            <el-form-item  class="selectMB10">
                <!--查询-->
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="queryByPage(pageIndex,pageSize)" v-text="nowLangue.query"></el-button>
            </el-form-item>
        </el-form>
        <!--报价列表：-->
        <label class="default-label" v-text="nowLangue.quotationList"></label>
        <el-table :data="tableData" border style="width: 100%; ">
            <!--序-->
            <el-table-column  type="index" header-align="center" align="center" :label="nowLangue.sequence"  width="40"></el-table-column>
            <!--操作-->
            <el-table-column  :label="nowLangue.operate" header-align="center" align="center" width="160">
                <template scope="scope">
                    <!--修改-->
                    <div class="operationItem bolder disabled" v-if="(scope.row.biddingStatus==20 ||scope.row.biddingStatus==30 || scope.row.biddingStatus==40 || scope.row.biddingStatus==50) && powerFlag1"  v-text="nowLangue.update"> </div>
                    <div class="operationItem bolder" v-else-if="!(scope.row.biddingStatus==20 ||scope.row.biddingStatus==30 || scope.row.biddingStatus==40 || scope.row.biddingStatus==50) && powerFlag1">
                        <!--修改-->
                        <router-link :to="{ path: 'Quotation', query: { quotationNumber: scope.row.quotationNumber}}" class="operationItem bolder" v-text="nowLangue.update"></router-link>
                    </div>
                    <div class="operationItem bolder"  v-if="scope.row.biddingStatus==20 && powerFlag2">
                        <!--审核-->
                        <router-link :to="{ path: 'CheckQuotation', query: { quotationNumber: scope.row.quotationNumber}}" class="operationItem bolder" v-text="nowLangue.toExamine"></router-link>
                    </div>
                    <!--审核-->
                    <div class="operationItem bolder disabled" v-else-if="scope.row.biddingStatus!=20 && powerFlag2" v-text="nowLangue.toExamine"></div>

                    <div class="operationItem bolder" v-if="scope.row.biddingStatus==30 && powerFlag3">
                        <!-- 审批-->
                        <router-link :to="{ path: 'CheckQuotation', query: { quotationNumber: scope.row.quotationNumber}}" class="operationItem bolder" v-text="nowLangue.approval"></router-link>
                    </div>
                    <!--审批-->
                    <div class="operationItem bolder disabled" v-else-if="scope.row.biddingStatus!=30 && powerFlag3" v-text="nowLangue.approval"></div>

                    <div class="operationItem bolder" >
                        <!--查看-->
                        <router-link :to="{ path: 'CheckQuotation', query: { quotationNumber: scope.row.quotationNumber}}" class="operationItem bolder" v-text="nowLangue.lookUp"></router-link>
                    </div>
                </template>
            </el-table-column>
            <!--报价单号-->
            <el-table-column prop="quotationNumber" :label="nowLangue.quotationNo" header-align="center"  show-overflow-tooltip> </el-table-column>
            <!--报价总数量-->
            <el-table-column prop="totalQuantityQuoted" :label="nowLangue.totalQuantityQuoted"  header-align="center"> </el-table-column>
            <!--报价币别-->
            <el-table-column prop="currencyCodeWord" :label="nowLangue.quotedCurrency"  header-align="center" show-overflow-tooltip> </el-table-column>
            <!--报价不含税总金额-->
            <el-table-column prop="totalQuotedPrice" :label="nowLangue.totalQuotedPrice"   header-align="center" align="right"> </el-table-column>
            <!--报价含税总金额-->
            <el-table-column prop="totalQuotedTaxPrice" :label="nowLangue.totalQuotedTaxPrice"   header-align="center" align="right"> </el-table-column>
            <!--本币总金额-->
            <el-table-column prop="totalCurrencyAmount" :label="nowLangue.totalCurrencyAmount"  header-align="center" align="right"> </el-table-column>
            <!--报价时间-->
            <el-table-column prop="createTime" :label="nowLangue.quotationTime"  header-align="center" show-overflow-tooltip> </el-table-column>
            <!--报价生效日期-->
            <el-table-column prop="startTime" :label="nowLangue.takeEffectiveDateOfQuotation"  header-align="center" show-overflow-tooltip> </el-table-column>
            <!--报价失效日期-->
            <el-table-column prop="endTime" :label="nowLangue.expiryDateOfQuotation"  header-align="center" show-overflow-tooltip> </el-table-column>
            <!--状态-->
            <el-table-column prop="biddingStatusName" :label="nowLangue.status"  width="80" header-align="center" align="center"  show-overflow-tooltip> </el-table-column>
            <!--报价人-->
            <el-table-column prop="bidderName" :label="nowLangue.bidderName"  width="60" header-align="center"  show-overflow-tooltip> </el-table-column>
            <!--审核人-->
            <el-table-column prop="auditorName" :label="nowLangue.auditorName"  width="60" header-align="center" show-overflow-tooltip > </el-table-column>
            <!--审批人-->
            <el-table-column prop="approverName" :label="nowLangue.approverName"  width="60" header-align="center"  show-overflow-tooltip> </el-table-column>
        </el-table>

        <!--分页-->
        <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page.sync="currentPage3" :current-page="pageIndex"
                       :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                       :page-count="totalPage" :total="totalNumber">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                nowLangue:{},
                powerFlag:false,   //不是业务合作人员
                quotationSheetForm:{
                    label:'',
                    value:'',
                    legalPersonName:'',
                    legalPersonId:'',
                    vendor:'',
                    quotationNumber:'',
                    spareCoding:'',
                    spareName:'',
                    biddingStatus:'',
                    biddingType:'',
                    createStartTime:'',
                    createEndTime:'',
                    quotationDate:'',
                    startTime:'',
                    endTime:'',
                    selectDropdown:[]
                },
                tableData:[],
                legalPersonDropDown:[], //法人名称
                supplierNameDropDown:[], //供应商名称
                selectDropdown:[],     //查询下拉菜单
                isPaging:true,
                pageIndex: 1,    //当前页（必传）
                pageSize: 10,   //每页多少条
                totalNumber: 20,
                totalPage: 2,
                currentPage3:1,
                //权限
                powerFlag1:false,
                powerFlag2:false,
                powerFlag3:false,
                //查询禁用
                selectDisabled:false
            }
        },
        created:function(){
            var _this=this;
            _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
        },
        computed:{
            identity(){
                return this.$store.state.identity;
            },
            powerData(){   //登录人员的权限
                return this.$store.state.powerData;
            },
            languageType(){
                return this.$store.state.languageType;
            }
        },
        watch:{
            languageType(){
                var _this=this;
                _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
            },
            identity(){
                var _this=this;
                if(_this.identity == 1){  //是本部人员
                    _this.legalPersonDropDownValue();
                }
                if(_this.identity == 2){
                    _this.powerFlag = true;   //是业务合作人员
                    _this.getQuotationUnit();  //获取当前登录用户对应的供应商
                }
            },
            powerData(){
                var _this = this;
                for(var i=0;i<_this.powerData.length;i++){
                    if(_this.powerData[i]=="ROLE_security_quotationSheet_operation"){
                        //报价清单修改、保存及提交权限
                        _this.powerFlag1=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_quotationSheet_review"){
                        //报价清单审核权限
                        _this.powerFlag2=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_quotationSheet_approve"){
                        //报价清单审批权限
                        _this.powerFlag3=true;
                    }
                }
            }    
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'QuotationSheet');
            // //ajax前后查询按钮禁用
            // $(".selectAll").ajaxStart(function(){
            //     _this.selectDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //     _this.selectDisabled=false;
            // });
            _this.inquiryDropdownValue();
            _this.queryByPage(_this.pageIndex, _this.pageSize);
            if(_this.identity == 1){  //是本部人员
                _this.legalPersonDropDownValue();
            }
            if(_this.identity == 2){
                _this.powerFlag = true;   //是业务合作人员
                _this.getQuotationUnit();  //获取当前登录用户对应的供应商
            }
            // 当前登录人员的权限
            for(var i=0;i<_this.powerData.length;i++){
                if(_this.powerData[i]=="ROLE_security_quotationSheet_operation"){
                    //报价清单修改、保存及提交权限
                    _this.powerFlag1=true;
                }
                if(_this.powerData[i]=="ROLE_security_quotationSheet_review"){
                    //报价清单审核权限
                    _this.powerFlag2=true;
                }
                if(_this.powerData[i]=="ROLE_security_quotationSheet_approve"){
                    //报价清单审批权限
                    _this.powerFlag3=true;
                }
            }
        },
        methods:{
            // 获取当前登录用户对应的供应商
            getQuotationUnit(){     //报价单位
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"quotation/findByVendorName",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.quotationSheetForm.vendorId = data.data[0].id;
                            _this.quotationSheetForm.vendorCode = data.data[0].partnerCode;
                            _this.quotationSheetForm.vendor = data.data[0].partnerName;
                            /*if(_this.quotationSheetForm.selectDropdown.length ==1){
                                _this.quotationSheetForm.vendor = _this.quotationSheetForm.selectDropdown[0].partnerCode;
                            }*/
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //报价日期
            selectedCreateStartTime(val){
                var _this=this;
                _this.quotationSheetForm.createStartTime = val;
                if(_this.quotationSheetForm.createEndTime && val > _this.quotationSheetForm.createEndTime){
                    var err=_this.languageType=="zh"?'报价生效日期不能大于报价失效日期':'The effective date of the quotation shall not be greater than the expiration date of the offer';
                    _this.common.errorTip(err);
                    _this.quotationSheetForm.createStartTime = "";
                };
            },
            selectedCreateEndTime(val){
                var _this=this;
                _this.quotationSheetForm.createEndTime = val;
                if(_this.quotationSheetForm.createStartTime && val < _this.quotationSheetForm.createStartTime){
                    var err=_this.languageType=="zh"?'报价失效日期不能小于报价生效日期':'The bid expiry date shall not be less than the effective date of the quotation';
                    _this.common.errorTip(err);
                    _this.quotationSheetForm.createEndTime = "";
                };
            },
            //报价生效日期
            selectedStartTime(val){
                var _this=this;
                _this.quotationSheetForm.startTime = val;
                if(_this.quotationSheetForm.endTime && val > _this.quotationSheetForm.endTime){
                    var err=_this.languageType=="zh"?'报价生效日期不能大于报价失效日期':'The effective date of the quotation shall not be greater than the expiration date of the offer';
                    _this.common.errorTip(err);
                    _this.quotationSheetForm.startTime = "";
                };
            },
            // 报价失效日期
            selectedEndTime(val){
                var _this=this;
                _this.quotationSheetForm.endTime = val;
                if(_this.quotationSheetForm.startTime && val < _this.quotationSheetForm.startTime){
                    var err=_this.languageType=="zh"?'报价失效日期不能小于报价生效日期':'The bid expiry date shall not be less than the effective date of the quotation';
                    _this.common.errorTip(err);
                    _this.quotationSheetForm.endTime = "";
                };
            },
            legalPersonDropDownValue(){  //法人名称下拉列表值
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"legalPerson/ofCurrentUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.legalPersonDropDown = data.data;
                            if(_this.legalPersonDropDown.length == 1){
                                _this.quotationSheetForm.legalPersonName = _this.legalPersonDropDown[0].legalPersonName;
                                _this.quotationSheetForm.legalPersonId = _this.legalPersonDropDown[0].id;
                                _this.dropdownValue();
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            changeLegalPerson(){
                var _this = this;
                for(var i=0;i<_this.legalPersonDropDown.length;i++){
                    if(_this.quotationSheetForm.legalPersonName==_this.legalPersonDropDown[i].legalPersonName){
                        _this.quotationSheetForm.legalPersonId=_this.legalPersonDropDown[i].id;
                    }
                }
                _this.dropdownValue();
            },
            dropdownValue(){  //供应商下拉列表值
                var _this=this;
                $.ajax({
                    type:'GET',
                    url:_this.metadata_service+"partner/queryByLegalPerson?legalPersonId="+_this.quotationSheetForm.legalPersonId,
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.quotationSheetForm.selectDropdown=data.data;
                            /*if(_this.quotationSheetForm.selectDropdown.length ==1){
                                _this.quotationSheetForm.vendor = _this.quotationSheetForm.selectDropdown[0].partnerCode;
                            }*/
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            inquiryDropdownValue(){  //类型和状态
                var _this = this;
//                var tmp=["type","biddingStatus"];
                var tmp=["biddingStatus"];
                $.ajax({
                    type:'POST',
                    url:_this.procurement_service+'procurement/getPullDownList',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType:"application/json;charset=utf-8",
                    success:function(data){
                        if(data.success){
                            _this.selectDropdown=data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //   分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this = this;
                _this.pageSize=val;
                _this.queryByPage(_this.pageIndex, _this.pageSize);
            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this = this;
                _this.pageIndex=val;
                _this.queryByPage(_this.pageIndex, _this.pageSize);
            },
            //查询按钮点击事件
            queryByPage(pageIndex,pageSize){
                var _this = this;
                _this.loading=true;
                var tmp = {
                    legalPersonId:_this.quotationSheetForm.legalPersonId,
                    vendorCode: _this.quotationSheetForm.vendor,
                    quotationNumber: _this.quotationSheetForm.quotationNumber,
                    spareCoding: _this.quotationSheetForm.spareCoding,
                    spareName: _this.quotationSheetForm.spareName,
                    biddingStatus: _this.quotationSheetForm.biddingStatus,
                    biddingType: _this.quotationSheetForm.biddingType,
                    createStartTime: _this.quotationSheetForm.createStartTime,
                    createEndTime: _this.quotationSheetForm.createEndTime,
                    quotationDate: _this.quotationSheetForm.quotationDate,
                    startTime: _this.quotationSheetForm.startTime,
                    endTime: _this.quotationSheetForm.endTime,
                    pageIndex: pageIndex,
                    pageSize: pageSize
                };
                _this.selectDisabled=true;
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service + "quotationSheet/search",
                    cache: false,
                    data: JSON.stringify(tmp),
                    dataType: 'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.loading=false;
                            var arr=data.data.dataList;
                            //金额保留两位
                            for(var i=0;i<arr.length;i++){
                                if(arr[i].totalQuotedPrice!=null){
                                    arr[i].totalQuotedPrice=arr[i].totalQuotedPrice.toFixed(2);     //报价不含税总金额
                                }
                                if(arr[i].totalQuotedTaxPrice!=null){
                                    arr[i].totalQuotedTaxPrice=arr[i].totalQuotedTaxPrice.toFixed(2); // 报价含税总金额
                                }
                                if(arr[i].totalCurrencyAmount!=null){
                                    arr[i].totalCurrencyAmount=arr[i].totalCurrencyAmount.toFixed(2);   //本币总金额
                                }

                            }
                            _this.tableData = arr;
                            _this.isPaging = true;
                            _this.totalPage = data.data.totalPage;
                            _this.totalNumber = data.data.totalNumber;
                        } else{
                            _this.common.errorTip(data.errorMessage);
                        }
                        _this.selectDisabled=false;
                    }
                });
            }
        }
    }
</script>